package com.gopay.bis.tbank.ebank.impl;

import java.util.HashMap;
import java.util.Map;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.tbank.ebank.operation.TBankB2cQuery;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.tbank.ebank.pay.TBankMessage;
import com.gopay.common.domain.bank.tbank.ebank.query.req.TBankOrderReq;
import com.gopay.common.domain.bank.tbank.ebank.query.res.TBankOrderRes;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpsTransport;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 电子银行业务请求接口
 * 
 * @TBankB2cBankServiceImpl.java
 * @author WangHongFei
 */
public class TBankB2cBankServiceImpl implements BankService{
	
	private TBankB2cQuery tbankB2cQuery;
	private HttpsTransport httpsTransport;
	 
	public String sign(BaseMessage message) throws GopayException{
		TBankMessage messageB2C = (TBankMessage) message;
		BankLogUtils.printB2CSignPlainLog(messageB2C);
		BankLogUtils.printB2CSignSignatureLog(messageB2C, messageB2C.getSignMessage());
		return messageB2C.getSignMessage();		
	}

	public Object verify(BaseMessage message) throws GopayException{
		TBankMessage messageB2C = (TBankMessage) message;
		BankLogUtils.printB2CVerifyPlainLog(messageB2C);
		BankLogUtils.printB2CVerifySignatureLog(messageB2C, messageB2C.getSignMessage());
		return true;
	}
	
	public Object query(Object object) throws GopayException{
		TBankOrderReq req = null;
		try {
			req = (TBankOrderReq) object;
			Map<String, String> map = new HashMap<String, String>();
			map.put("orderNo", req.getOrderNo());
			map.put("mercNo", req.getMercNo());
			map.put("msgType", req.getMsgType());
			map.put("txnType", req.getTxnType());
			map.put("signDate", req.getSignDate());			
			TBankOrderRes resMessage = new TBankOrderRes();
			httpsTransport.setUrl(req.getBankUrl());
			BankLogUtils.printB2CQuerySendLog(req, map);
			Object messageData = httpsTransport.submit(map);
			resMessage = tbankB2cQuery.parse(messageData);
			return resMessage;
		} catch (Exception e) {
			BankLogUtils.printB2CQueryExceptionLog(req, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6033);
		}
		
	}
	
	public void setTbankB2cQuery(TBankB2cQuery tbankB2cQuery) {
		this.tbankB2cQuery = tbankB2cQuery;
	}
	
	public void setHttpsTransport(HttpsTransport httpsTransport) {
		this.httpsTransport = httpsTransport;
	}

	@Override
	public Object dayend(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
